﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Data;
using BLL;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CardCenter_ConsultationCardPwdSelect : System.Web.UI.Page
{
    ConsultationCardPwdSelect bllCardInfo = new ConsultationCardPwdSelect();
    WebUtility utility = new WebUtility();
    ReadParamtes bllParams = new ReadParamtes();
    SendSMS bllSendMsg = new SendSMS();

    protected void Page_Load(object sender, EventArgs e)
    {
        //选择开始及结束时间
        this.txt_StartTime.Attributes.Add("onfocus", "WdatePicker({skin:'ext',dateFmt:'yyyy-MM-dd HH:mm:ss'})");
        this.txt_EndTime.Attributes.Add("onfocus", "WdatePicker({skin:'ext',dateFmt:'yyyy-MM-dd HH:mm:ss'})");
    }

    //protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    //{
    //    Image1.ImageUrl = "~/Images/CardCenterImg/hui1.jpg";
    //    Image2.ImageUrl = "~/Images/CardCenterImg/cai2.jpg";
    //    Label1.ForeColor = System.Drawing.Color.Gray;
    //    Label2.ForeColor = System.Drawing.Color.Black;
    //}

    protected void btn_GetPwd1_Click(object sender, ImageClickEventArgs e)
    {
        string mobileNO = this.MobileNO.Text.Trim();//Request["MobileNO"].ToString();
        string verifyCode = this.txt_VerifyCode.Text.Trim();
        string cardNO = this.txt_CardNO.Text.Trim();
        string msgTemplate = bllParams.getParamValue("QueryCardPassWord");

        bool isRequestTimeOut = bllCardInfo.IsRequestTimedOut(mobileNO, verifyCode);
        if (isRequestTimeOut == false)
        {
            utility.AlertMsg("对不起，您所输入的验证码已过期！请重新申请。");
            this.MobileNO.Text = string.Empty;
            this.txt_VerifyCode.Text = string.Empty;
            this.txt_CardNO.Text = string.Empty;
        }
        else
        {
            bool isSame = bllCardInfo.IsSameToVerifyCodeAndMobileNO(mobileNO, verifyCode);
            if (isSame == false)
            {
                utility.AlertMsg("对不起，您所输入的验证码错误！请重新输入。");
                this.MobileNO.Text = string.Empty;
                this.txt_VerifyCode.Text = string.Empty;
                this.txt_CardNO.Text = string.Empty;
            }
            else
            {
                bool isExistsCard = bllCardInfo.IsExistsCardNOAndMobileNO(mobileNO, cardNO);
                if (isExistsCard == false)
                {
                    utility.AlertMsg("对不起，您所输入的卡号错误或者不存在！请重新输入。");
                    this.MobileNO.Text = string.Empty;
                    this.txt_VerifyCode.Text = string.Empty;
                    this.txt_CardNO.Text = string.Empty;
                }
                else
                {
                    DataTable dtCardInfo = bllCardInfo.GetCardPwd(cardNO);
                    string cardNo = dtCardInfo.Rows[0]["CPXX_KH"].ToString();
                    string cardPwd = dtCardInfo.Rows[0]["CPXX_MM"].ToString();

                    string sendMsg = WebUtility.GenrateSendSelectCardMsg(cardNO, cardPwd, msgTemplate);
                    int i = bllSendMsg.SendMsg(sendMsg, mobileNO,"2");
                    if (i > 0)
                    {
                        utility.AlertMsg("短信已发出，请注意查收！");
                        this.MobileNO.Text = string.Empty;
                        this.txt_VerifyCode.Text = string.Empty;
                        this.txt_CardNO.Text = string.Empty;

                        Image1.ImageUrl = "~/Images/CardCenterImg/hui1.jpg";
                        Image2.ImageUrl = "~/Images/CardCenterImg/cai2.jpg";
                        Label1.ForeColor = System.Drawing.Color.Gray;
                        Label2.ForeColor = System.Drawing.Color.Black;
                    }
                    else
                    {
                        utility.AlertMsg("短信发送失败，请联系管理员！");
                        this.MobileNO.Text = string.Empty;
                        this.txt_VerifyCode.Text = string.Empty;
                        this.txt_CardNO.Text = string.Empty;
                    }
                }
            }
        }
    }

    protected void btn_GetPwd2_Click(object sender, ImageClickEventArgs e)
    {
        string mobileNO = this.MobileNO.Text.Trim(); //Request["MobileNO"].ToString();
        string verifyCode = this.txt_VerifyCode2.Text.Trim();
        DateTime startTime = Convert.ToDateTime(this.txt_StartTime.Text); 
        DateTime endTime = Convert.ToDateTime(this.txt_EndTime.Text);

        string msgHeader = bllParams.getParamValue("SmsPurchaseHeader");
        string msgTemplate = bllParams.getParamValue("QueryCardPassWord");

        bool isRequestTimeOut = bllCardInfo.IsRequestTimedOut(mobileNO, verifyCode);
        if (isRequestTimeOut == false)
        {
            utility.AlertMsg("对不起，您所输入的验证码已过期！请重新申请。");
            this.MobileNO.Text = string.Empty;
            this.txt_VerifyCode2.Text = string.Empty;
            this.txt_StartTime.Text = string.Empty;
            this.txt_EndTime.Text = string.Empty;
        }
        else
        {
            bool isSame = bllCardInfo.IsSameToVerifyCodeAndMobileNO(mobileNO, verifyCode);
            if (isSame == false)
            {
                utility.AlertMsg("对不起，您所输入的验证码错误！请重新输入。");
                this.MobileNO.Text = string.Empty;
                this.txt_VerifyCode2.Text = string.Empty;
                this.txt_StartTime.Text = string.Empty;
                this.txt_EndTime.Text = string.Empty;
            }
            else
            {
                bool isHasRecords = bllCardInfo.IsExistsVerifyCodeAndMobileNO(mobileNO, startTime, endTime);
                if (isHasRecords == false)
                {
                    utility.AlertMsg("对不起，您在当前时间内不存在购卡纪录！");
                    this.MobileNO.Text = string.Empty;
                    this.txt_VerifyCode2.Text = string.Empty;
                    this.txt_StartTime.Text = string.Empty;
                    this.txt_EndTime.Text = string.Empty;
                }
                else
                {
                    DataTable dtCardInfo = bllCardInfo.ReturnCardNOAndPwd(mobileNO, startTime, endTime);
                    string sendMsgHead = WebUtility.GenratePurchaseCarsHeaderSms(dtCardInfo.Rows.Count, msgHeader);
                    bllSendMsg.SendMsg(sendMsgHead, mobileNO,"1");
                    for (int i = 0; i < dtCardInfo.Rows.Count; i++)
                    {
                        bllSendMsg.SendMsg(WebUtility.GenrateSendSelectCardMsg(dtCardInfo.Rows[i]["CPXX_KH"].ToString(), dtCardInfo.Rows[i]["CPXX_MM"].ToString(), msgTemplate), mobileNO,"1");
                    }
                    utility.AlertMsg("短信已发出，请注意查收！");
                    this.MobileNO.Text = string.Empty;
                    this.txt_VerifyCode2.Text = string.Empty;
                    this.txt_StartTime.Text = string.Empty;
                    this.txt_EndTime.Text = string.Empty;

                    Image1.ImageUrl = "~/Images/CardCenterImg/hui1.jpg";
                    Image2.ImageUrl = "~/Images/CardCenterImg/cai2.jpg";
                    Label1.ForeColor = System.Drawing.Color.Gray;
                    Label2.ForeColor = System.Drawing.Color.Black;
                }
            }
        }
    }    
}
